package com.qdlc.p2p.biz.processor.task;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qd.p2p.sina.pay.response.CreateHostingCollectTradeResponse;
import com.qd.p2p.sina.service.SinaPayService;
import com.qdlc.p2p.biz.processor.event.TppTradeLogValueEvent;
import com.qdlc.p2p.common.constant.SinaConstant;
import com.qdlc.p2p.common.constant.Summary;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.OrderNoUtils;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dao.BorrowTenderDao;
import com.qdlc.p2p.dal.dao.TppTradeLogDao;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.util.UserUtils;

@Service
@Transactional
public class TppTradeLogTaskProcessor extends AbstractTaskProcessor {

	private static final Logger log = Logger.getLogger(TppTradeLogTaskProcessor.class);
	
	@Resource
	BorrowTenderDao borrowTenderDao;
	
	@Resource
	TppTradeLogDao tppTradeLogDao;
	
	@Autowired
	SinaPayService sinaPayService;
	
	@Override
	public boolean supports(ValueEvent event) {
		return event instanceof TppTradeLogValueEvent;
	}

	@Override
	protected void process0(ValueEvent event) throws TaskProcessorException {
		TppTradeLogValueEvent tradeEvent = (TppTradeLogValueEvent)event;
		User user = tradeEvent.getUser();
		//平台手续费(普通用户投标后不收取)
		double webCashFee = tradeEvent.getWebCashFee(); 
		
		String tppUserCustId = UserUtils.getUserTppCustId(user.getUserId());
		//保存待收记录
		TppTradeLog tppTradeLog = new TppTradeLog(tppUserCustId,SystemConfigHelper.getValue(Nid.MEMBER_EMAIL), SinaConstant.IDENTITY_TYPE_UID, webCashFee,
				OrderNoUtils.getSerialNumber(), SinaConstant.TRADE_STATUS_WAIT_PAY, SinaConstant.TRADE_CODE_1001, SinaConstant.TRADE_SERVICE_CASH_FEE, "提现手续费");
        tppTradeLog.setAddTime(DateUtil.getNow());
        tppTradeLogDao.save(tppTradeLog);
        
        String serviceType = tppTradeLog.getServiceType();
        String identityId = tppUserCustId;
        String goodsId = String.valueOf(tppTradeLog.getBorrowId());
        double amount = tppTradeLog.getMoney();
        String canRepayOnFailed = SinaConstant.SINA_YES;
        String payerIp = tradeEvent.getPayerIp();
        String summary = Summary.getSummaryNameByType(serviceType);
        CreateHostingCollectTradeResponse response = sinaPayService.createHostingCollectTrade(identityId, goodsId, amount, canRepayOnFailed, payerIp, summary);
        String result = response.success() ? "成功" : "失败";
        log.info(MessageFormat.format("++++[用户{0}][提现{1}]:[提现{2}元,新浪手续费{3}元,平台收费{4}元]", user.getUserName(), result, tradeEvent.getMoney(), tradeEvent.getServFee(), tradeEvent.getWebCashFee()));
	}

}
